home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / EXTERN.C < prev    next >
C/C++ Source or Header  |  1992-01-20  |  7KB  |  212 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /scheme/src/microcode/RCS/extern.c,v 9.32 1992/01/20 16:06:21 jinx Exp $
  4.  
  5. Copyright (c) 1987-1992 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. #include "scheme.h"
  36. #include "prims.h"
  37.  
  38. /* Mapping between the internal and external representations of
  39.    primitives and return addresses.  */
  40.  
  41. DEFINE_PRIMITIVE ("MAP-CODE-TO-MACHINE-ADDRESS", Prim_map_code_to_address, 2, 2,
  42.   "For return codes and primitives, this returns the internal\n\
  43. representation of the return address or primitive address given the\n\
  44. external representation.\n\
  45. \n\
  46. This accepts two arguments, TYPE-CODE and VALUE-CODE.  TYPE-CODE is\n\
  47. the microcode type of the object to be returned; it must be either a\n\
  48. return address or primitive procedure type.  VALUE-CODE is the index\n\
  49. number (i.e. external representation) of the desired result.")
  50. {
  51.   long tc, number;
  52.   PRIMITIVE_HEADER (2);
  53.   tc = (arg_nonnegative_integer (1));
  54.   number = (arg_nonnegative_integer (2));
  55.   switch (tc)
  56.   {
  57.     case TC_RETURN_CODE:
  58.       if (number > MAX_RETURN_CODE)
  59.     error_bad_range_arg (2);
  60.       PRIMITIVE_RETURN (MAKE_OBJECT (tc, number));
  61.  
  62.     case TC_PRIMITIVE:
  63.       if (number >= (NUMBER_OF_PRIMITIVES ()))
  64.     error_bad_range_arg (2);
  65.       PRIMITIVE_RETURN
  66.     ((number > MAX_PRIMITIVE)
  67.      ? (MAKE_PRIMITIVE_OBJECT (number, (MAX_PRIMITIVE + 1)))
  68.      : (MAKE_PRIMITIVE_OBJECT (0, number)));
  69.  
  70.     default:
  71.       error_bad_range_arg (1);
  72.   }
  73.   /* NOTREACHED */
  74. }
  75.  
  76. DEFINE_PRIMITIVE ("MAP-MACHINE-ADDRESS-TO-CODE", Prim_map_address_to_code, 2, 2,
  77.   "This is the inverse operation of `map-code-to-machine-address'.  Given\n\
  78. a machine ADDRESS and a TYPE-CODE (either return code or primitive\n\
  79. procedure), it finds the number for the external representation for\n\
  80. the internal address.")
  81. {
  82.   fast long tc;
  83.   fast SCHEME_OBJECT address;
  84.   PRIMITIVE_HEADER (2);
  85.   tc = (arg_nonnegative_integer (1));
  86.   address = (ARG_REF (2));
  87.   if ((OBJECT_TYPE (address)) != tc)
  88.     error_wrong_type_arg (2);
  89.   switch (tc)
  90.     {
  91.     case TC_RETURN_CODE:
  92.       {
  93.     fast long number = (OBJECT_DATUM (address));
  94.     if (number > MAX_RETURN_CODE)
  95.       error_bad_range_arg (2);
  96.     PRIMITIVE_RETURN (LONG_TO_UNSIGNED_FIXNUM (number));
  97.       }
  98.  
  99.     case TC_PRIMITIVE:
  100.       PRIMITIVE_RETURN (LONG_TO_UNSIGNED_FIXNUM (PRIMITIVE_NUMBER (address)));
  101.  
  102.     default:
  103.       error_bad_range_arg (1);
  104.     }
  105.   /* NOTREACHED */
  106. }
  107.  
  108. DEFINE_PRIMITIVE ("PRIMITIVE-PROCEDURE-ARITY", Prim_primitive_procedure_arity, 1, 1,
  109.   "Given a primitive procedure, returns the number of arguments it requires.")
  110. {
  111.   PRIMITIVE_HEADER (1);
  112.   CHECK_ARG (1, PRIMITIVE_P);
  113.   {
  114.     fast SCHEME_OBJECT primitive = (ARG_REF (1));
  115.     extern long EXFUN (primitive_to_arity, (SCHEME_OBJECT));
  116.     if ((PRIMITIVE_NUMBER (primitive)) >= (NUMBER_OF_PRIMITIVES ()))
  117.       error_bad_range_arg (1);
  118.     PRIMITIVE_RETURN (LONG_TO_FIXNUM (primitive_to_arity (primitive)));
  119.   }
  120. }
  121.  
  122. DEFINE_PRIMITIVE ("PRIMITIVE-PROCEDURE-DOCUMENTATION", Prim_primitive_procedure_doc, 1, 1,
  123.   "Given a primitive procedure, return its documentation string.")
  124. {
  125.   PRIMITIVE_HEADER (1);
  126.   CHECK_ARG (1, PRIMITIVE_P);
  127.   {
  128.     fast SCHEME_OBJECT primitive = (ARG_REF (1));
  129.     if ((PRIMITIVE_NUMBER (primitive)) >= (NUMBER_OF_PRIMITIVES ()))
  130.       error_bad_range_arg (1);
  131.     {
  132.       extern char * EXFUN (primitive_to_documentation, (SCHEME_OBJECT));
  133.       fast char * answer = (primitive_to_documentation (primitive));
  134.       PRIMITIVE_RETURN
  135.     ((answer == ((char *) 0))
  136.      ? SHARP_F
  137.      : (char_pointer_to_string ((unsigned char *) answer)));
  138.     }
  139.   }
  140. }
  141.  
  142. DEFINE_PRIMITIVE ("GET-PRIMITIVE-COUNTS", Prim_get_primitive_counts, 0, 0,
  143.   "Return a pair of integers which are the number of primitive procedures.\n\
  144. The car is the count of defined primitives;\n\
  145. the cdr is the count of undefined primitives that are referenced.")
  146. {
  147.   PRIMITIVE_HEADER (0);
  148.   PRIMITIVE_RETURN
  149.     (cons ((LONG_TO_UNSIGNED_FIXNUM (NUMBER_OF_DEFINED_PRIMITIVES ())),
  150.        (LONG_TO_UNSIGNED_FIXNUM (NUMBER_OF_UNDEFINED_PRIMITIVES ()))));
  151. }
  152.  
  153. DEFINE_PRIMITIVE ("GET-PRIMITIVE-NAME", Prim_get_primitive_name, 1, 1,
  154.   "Return the (string) name of PRIMITIVE-PROCEDURE.")
  155. {
  156.   PRIMITIVE_HEADER (1);
  157.   {
  158.     fast SCHEME_OBJECT primitive = (ARG_REF (1));
  159.     if (! ((PRIMITIVE_P (primitive)) || (FIXNUM_P (primitive))))
  160.       error_wrong_type_arg (1);
  161.     {
  162.       fast long number = (PRIMITIVE_NUMBER (primitive));
  163.       extern SCHEME_OBJECT EXFUN (primitive_name, (int));
  164.       if ((number < 0) || (number >= NUMBER_OF_PRIMITIVES()))
  165.     error_bad_range_arg (1);
  166.       PRIMITIVE_RETURN (primitive_name (number));
  167.     }
  168.   }
  169. }
  170.  
  171. DEFINE_PRIMITIVE ("GET-PRIMITIVE-ADDRESS", Prim_get_primitive_address, 2, 2,
  172.   "Given a symbol NAME, return the primitive object of that name.\n\
  173. ARITY is the number of arguments which the primitive should expect.\n\
  174. If ARITY is #F, #F is returned if the primitive is not implemented,\n\
  175. even if the name already exists.\n\
  176. If ARITY is an integer, a primitive object will always be returned,\n\
  177. whether the corresponding primitive is implemented or not.")
  178. {
  179.   fast SCHEME_OBJECT name;
  180.   fast SCHEME_OBJECT arity_arg;
  181.   extern SCHEME_OBJECT EXFUN
  182.     (find_primitive, (SCHEME_OBJECT, Boolean, Boolean, int));
  183.   Boolean intern_p, allow_p;
  184.   long arity;
  185.   PRIMITIVE_HEADER (2);
  186.   CHECK_ARG (1, SYMBOL_P);
  187.   name = (ARG_REF (1));
  188.   TOUCH_IN_PRIMITIVE ((ARG_REF (2)), arity_arg);
  189.   if (arity_arg == SHARP_F)
  190.     {
  191.       allow_p = false;
  192.       intern_p = false;
  193.       arity = UNKNOWN_PRIMITIVE_ARITY;
  194.     }
  195.   else if (arity_arg == SHARP_T)
  196.     {
  197.       allow_p = true;
  198.       intern_p = false;
  199.       arity = UNKNOWN_PRIMITIVE_ARITY;
  200.     }
  201.   else
  202.     {
  203.       CHECK_ARG(2, FIXNUM_P);
  204.       allow_p = true;
  205.       intern_p = true;
  206.       arity = (FIXNUM_TO_LONG (arity_arg));
  207.     }
  208.   PRIMITIVE_RETURN
  209.     (find_primitive
  210.      ((FAST_MEMORY_REF (name, SYMBOL_NAME)), intern_p, allow_p, arity));
  211. }
  212.